/**
 * 打印管理
 */
// #ifdef APP-PLUS
import {usePluginStoreWithOut} from '@/store/plugin'
const plugin = usePluginStoreWithOut()
import * as module from "@/uni_modules/leven-uts-printer"
// #endif

export class PrinterManager {
  constructor() { }

  //关闭连接
  close () {
    module.close(res => {
      if (res.code == 0) {
        plugin.isConnect = false
        plugin.connectDeviceName = ""
      } else {
        console.log(res.message)
        uni.$u.toast(res.message);
      }
    })
  }

  //初始化打印机
  initPrinter () {
    return new Promise(resolve => {
      module.initPrinter(res => {
        resolve(res)
      })
    })
  }

  //打印文本
  printText (content) {
    return new Promise(resolve => {
      module.printText({
        content: content
      }, res => {
        resolve(res)
      })
    })
  }

  //设置打印属性
  setPrinter (command, value) {
    return new Promise(resolve => {
      module.setPrinter({
        command: command,
        value: value
      }, res => {
        resolve(res)
      })
    })
  }

  //设置字体样式
  setFont (characterType, width, height, bold, underline) {
    return new Promise(resolve => {
      module.setFont({
        characterType: characterType,
        width: width,
        height: height,
        bold: bold,
        underline: underline
      }, res => {
        resolve(res)
      })
    })
  }

  //发送字节数据
  sendBytesData (bytes) {
    return new Promise(resolve => {
      module.sendBytesData({
        content: bytes
      }, res => {
        resolve(res)
      })
    })
  }

  //切纸
  cutPaper (isHalf) {
    return new Promise(resolve => {
      module.cutPaper({
        isHalf: isHalf
      }, res => {
        resolve(res)
      })
    })
  }

  //打印表格
  printTable (column, regular, columnWidth, rows) {
    return new Promise(resolve => {
      module.printTable({
        column: column,
        regular: regular,
        columnWidth: columnWidth,
        rows: rows
      }, res => {
        resolve(res)
      })
    })
  }

  //获取纸张宽度
  getPaperWidth () {
    return new Promise(resolve => {
      module.getPaperWidth(res => {
        resolve(res)
      })
    })
  }

  //设置纸张宽度
  setPaperWidth (width) {
    return new Promise(resolve => {
      module.setPaperWidth({
        width: width
      }, res => {
        resolve(res)
      })
    })
  }

  //查询打印机状态
  getCurrentStatus () {
    return new Promise(resolve => {
      module.getCurrentStatus(res => {
        resolve(res)
      })
    })
  }

  //页设置
  pageSetup (paperType, pageWidth, pageHeight) {
    return new Promise(resolve => {
      module.pageSetup({
        paperType: paperType,
        pageWidth: pageWidth,
        pageHeight: pageHeight
      }, res => {
        resolve(res)
      })
    })
  }

  //画文字
  drawTextForArea (areaStartX, areaStartY, areaEndX, areaEndY, xAlign, yAlign, text, fontSize, bold, reverse, underline, deleteline, rotate) {
    return new Promise(resolve => {
      module.drawTextForArea({
        areaStartX: areaStartX,
        areaStartY: areaStartY,
        areaEndX: areaEndX,
        areaEndY: areaEndY,
        xAlign: xAlign,
        yAlign: yAlign,
        text: text,
        fontSize: fontSize,
        bold: bold,
        reverse: reverse,
        underline: underline,
        deleteline: deleteline,
        rotate: rotate
      }, res => {
        resolve(res)
      })
    })
  }

  //画二维码
  drawQrCode (startX, startY, text, rotate, ver, level) {
    return new Promise(resolve => {
      module.drawQrCode({
        startX: startX,
        startY: startY,
        text: text,
        rotate: rotate,
        ver: ver,
        level: level
      }, res => {
        resolve(res)
      })
    })
  }

  //画条码
  drawBarCode (startX, startY, text, barcodeType, lineWidth, height, rotate) {
    return new Promise(resolve => {
      module.drawBarCode({
        startX: startX,
        startY: startY,
        text: text,
        barcodeType: barcodeType,
        lineWidth: lineWidth,
        height: height,
        rotate: rotate
      }, res => {
        resolve(res)
      })
    })
  }

  //画区域条码
  drawBarCodeForArea (areaStartX, areaStartY, areaEndX, areaEndY, xAlign, yAlign, startX, startY, text, barcodeType, lineWidth, height, rotate) {
    return new Promise(resolve => {
      module.drawBarCodeForArea({
        areaStartX: areaStartX,
        areaStartY: areaStartY,
        areaEndX: areaEndX,
        areaEndY: areaEndY,
        xAlign: xAlign,
        yAlign: yAlign,
        startX: startX,
        startY: startY,
        text: text,
        barcodeType: barcodeType,
        lineWidth: lineWidth,
        height: height,
        rotate: rotate
      }, res => {
        resolve(res)
      })
    })
  }

  //画图
  drawGraphic (startX, startY, path, type, zoomImage, newWidth) {
    return new Promise(resolve => {
      module.drawGraphic({
        startX: startX,
        startY: startY,
        path: path,
        type: type,
        zoomImage: zoomImage,
        newWidth: newWidth
      }, res => {
        resolve(res)
      })
    })
  }

  //打印
  print (rotate, skip) {
    return new Promise(resolve => {
      module.print({
        rotate: rotate,
        skip: skip
      }, res => {
        resolve(res)
      })
    })
  }

  //画边框
  drawBorder (lineWidth, topLeftX, topLeftY, bottomRightX, bottomRightY) {
    return new Promise(resolve => {
      module.drawBorder({
        lineWidth: lineWidth,
        topLeftX: topLeftX,
        topLeftY: topLeftY,
        bottomRightX: bottomRightX,
        bottomRightY: bottomRightY
      }, res => {
        resolve(res)
      })
    })
  }

  //画线
  drawLine (lineWidth, startX, startY, endX, endY, isSolidLine) {
    return new Promise(resolve => {
      module.drawLine({
        lineWidth: lineWidth,
        startX: startX,
        startY: startY,
        endX: endX,
        endY: endY,
        isSolidLine: isSolidLine
      }, res => {
        resolve(res)
      })
    })
  }

  //TSPL设置标签间的缝隙大小
  setGAPTSPL (value, offset) {
    return new Promise(resolve => {
      module.setGAPTSPL({
        value: value,
        offset: offset
      }, res => {
        resolve(res)
      })
    })
  }

  //TSPL标签设置
  setPrinterTSPL (command, value) {
    return new Promise(resolve => {
      module.setPrinterTSPL({
        command: command,
        value: value
      }, res => {
        resolve(res)
      })
    })
  }

  //页设置TSPL
  pageSetupTSPL (paperSize, pageWidth, pageHeight) {
    return new Promise(resolve => {
      module.pageSetupTSPL({
        paperSize: paperSize,
        pageWidth: pageWidth,
        pageHeight: pageHeight
      }, res => {
        resolve(res)
      })
    })
  }

  //TSPL标签发送指令
  sendStrToPrinterTSPL (content) {
    return new Promise(resolve => {
      module.sendStrToPrinterTSPL({
        content: content
      }, res => {
        resolve(res)
      })
    })
  }

  //TSPL标签打印文本
  drawTextTSPL (startX, startY, isSimplifiedChinese, xMultiplication, yMultiplication, rotate, text) {
    return new Promise(resolve => {
      module.drawTextTSPL({
        startX: startX,
        startY: startY,
        isSimplifiedChinese: isSimplifiedChinese,
        xMultiplication: xMultiplication,
        yMultiplication: yMultiplication,
        rotate: rotate,
        text: text
      }, res => {
        resolve(res)
      })
    })
  }

  //TSPL标签区域打印文本
  drawTextTSPLForArea (areaStartX, areaStartY, areaEndX, areaEndY, xAlign, yAlign, isSimplifiedChinese, xMultiplication, yMultiplication, rotate,
    text) {
    return new Promise(resolve => {
      module.drawTextTSPLForArea({
        areaStartX: areaStartX,
        areaStartY: areaStartY,
        areaEndX: areaEndX,
        areaEndY: areaEndY,
        xAlign: xAlign,
        yAlign: yAlign,
        isSimplifiedChinese: isSimplifiedChinese,
        xMultiplication: xMultiplication,
        yMultiplication: yMultiplication,
        rotate: rotate,
        text: text
      }, res => {
        resolve(res)
      })
    })
  }

  drawLineTSPL (startX, startY, lineLength, lineHeight) {
    return new Promise(resolve => {
      module.drawLineTSPL({
        startX: startX,
        startY: startY,
        lineLength: lineLength,
        lineHeight: lineHeight
      }, res => {
        resolve(res)
      })
    })
  }

  draw2DBarCodeTSPL (startX, startY, barcodeType, level, width, rotate, text) {
    return new Promise(resolve => {
      module.draw2DBarCodeTSPL({
        startX: startX,
        startY: startY,
        barcodeType: barcodeType,
        level: level,
        width: width,
        rotate: rotate,
        text: text
      }, res => {
        resolve(res)
      })
    })
  }

  drawBarCodeTSPL (startX, startY, barcodeType, height, isReadable, rotate, narrowWidth, wideWidth, text) {
    return new Promise(resolve => {
      module.drawBarCodeTSPL({
        startX: startX,
        startY: startY,
        barcodeType: barcodeType,
        height: height,
        isReadable: isReadable,
        rotate: rotate,
        narrowWidth: narrowWidth,
        wideWidth: wideWidth,
        text: text
      }, res => {
        resolve(res)
      })
    })
  }

  //TSPL标签开启钱箱
  openCashBoxTSPL (t1, t2) {
    return new Promise(resolve => {
      module.openCashBoxTSPL({
        t1: t1,
        t2: t2
      }, res => {
        resolve(res)
      })
    })
  }

  //通用方法打开钱箱
  openCashBox (t1, t2) {
    console.log('打开钱箱')
    return new Promise(resolve => {
      module.openCashBox({
        t1: t1,
        t2: t2
      }, res => {
        resolve(res)
      })
    })
  }

  //TSPL标签打印
  printTSPL (numbers) {
    return new Promise(resolve => {
      module.printTSPL({
        numbers: numbers
      }, res => {
        resolve(res)
      })
    })
  }

  //TSPL标签蜂鸣器
  beepTSPL (level, interval) {
    return new Promise(resolve => {
      module.beepTSPL({
        level: level,
        interval: interval
      }, res => {
        resolve(res)
      })
    })
  }

  //TSPL标签打印边框
  drawBorderTSPL (lineWidth, topLeftX, topLeftY, bottomRightX, bottomRightY) {
    return new Promise(resolve => {
      module.drawBorderTSPL({
        lineWidth: lineWidth,
        topLeftX: topLeftX,
        topLeftY: topLeftY,
        bottomRightX: bottomRightX,
        bottomRightY: bottomRightY
      }, res => {
        resolve(res)
      })
    })
  }

  //TSPL标签区域图片打印
  drawBitmapTSPLForArea (areaStartX, areaStartY, areaEndX, areaEndY, xAlign, yAlign, mode, path, type, zoomImage, newWidth) {
    return new Promise(resolve => {
      module.drawBitmapTSPLForArea({
        areaStartX: areaStartX,
        areaStartY: areaStartY,
        areaEndX: areaEndX,
        areaEndY: areaEndY,
        xAlign: xAlign,
        yAlign: yAlign,
        mode: mode,
        path: path,
        type: type,
        zoomImage: zoomImage,
        newWidth: newWidth
      }, res => {
        resolve(res)
      })
    })
  }

  //TSPL标签翻转区域打印
  reverseAreaTSPL (startX, startY, width, height) {
    return new Promise(resolve => {
      module.reverseAreaTSPL({
        startX: startX,
        startY: startY,
        width: width,
        height: height
      }, res => {
        resolve(res)
      })
    })
  }

  //十六进制字符串转字节数组
  hexStringToByteArray (content) {
    return new Promise(resolve => {
      module.hexStringToByteArray({
        content: content
      }, res => {
        resolve(res)
      })
    })
  }

  //字符串转16进制
  stringToHex (content) {
    return new Promise(resolve => {
      module.stringToHex({
        content: content
      }, res => {
        resolve(res)
      })
    })
  }

  //打印条码或二维码
  printBarCode (type, width, height, level, content) {
    return new Promise(resolve => {
      module.printBarCode({
        type: type,
        width: width,
        height: height,
        level: level,
        content: content
      }, res => {
        resolve(res)
      })
    })
  }

  //打印图片带等级
  printImageWidthLevel (path, type, alignType, left, level) {
    module.printImageWidthLevel({
      path: path,
      type: type,
      alignType: alignType,
      left: left,
      level: level
    }, res => {
      resolve(res)
    })
  }
}
